package org.flowers.unbeaten.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.flowers.unbeaten.base.entity.BaseEntity;
import org.flowers.unbeaten.logging.aop.annotation.Logging;
import org.flowers.unbeaten.logging.aop.enums.BusinessType;
import org.flowers.unbeaten.base.entity.User;
import org.flowers.unbeaten.service.IUserService;
import org.flowers.unbeaten.util.DataResult;
import org.flowers.unbeaten.util.HttpSessionUtils;
import org.flowers.unbeaten.base.vo.LoginVo;
import org.flowers.unbeaten.base.vo.PasswordVo;
import org.flowers.unbeaten.base.vo.UserRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author RyanWang
 * @since 2021-07-15
 */
@Api(tags = "后台系统-用户管理")
@RestController
@RequestMapping("/user/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private HttpSessionUtils httpSessionUtils;

    @PostMapping("/list")
    @ApiOperation(value = "分页获取用户列表接口")
    @Logging(title = "用户管理", describe = "分页获取用户列表", type = BusinessType.QUERY)
    @RequiresPermissions("user:user:list")
    public DataResult list(@RequestBody User user, BaseEntity baseEntity) {
        return DataResult.success(userService.getUserPageList(user, baseEntity));
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录接口")
    @Logging(title = "用户管理", describe = "用户登录", type = BusinessType.OTHER)
    public DataResult login(@RequestBody LoginVo vo) {
        return DataResult.success(userService.login(vo));
    }

    @GetMapping("/logout")
    @ApiOperation(value = "用户退出接口")
    @Logging(title = "用户管理", describe = "用户退出", type = BusinessType.OTHER)
    public DataResult logout() {
        httpSessionUtils.abortCurrentUserByToken();
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return DataResult.success();
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增用户接口")
    @Logging(title = "用户管理", describe = "新增用户", type = BusinessType.ADD)
    @RequiresPermissions("user:user:add")
    public DataResult add(@RequestBody User user) {
        userService.add(user);
        return DataResult.success();
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑用户接口")
    @Logging(title = "用户管理", describe = "编辑用户", type = BusinessType.EDIT)
    @RequiresPermissions("user:user:edit")
    public DataResult edit(@RequestBody User user) {
        userService.edit(user);
        return DataResult.success();
    }

    @DeleteMapping("/delete/{ids}")
    @ApiOperation(value = "删除用户接口")
    @Logging(title = "用户管理", describe = "删除用户", type = BusinessType.REMOVE)
    @RequiresPermissions("user:user:delete")
    public DataResult delete(@PathVariable List<String> ids) {
        userService.delete(ids);
        return DataResult.success();
    }

    @GetMapping("/detail/{userId}")
    @ApiOperation(value = "用户详情接口")
    @Logging(title = "用户管理", describe = "用户详情", type = BusinessType.QUERY)
    @RequiresPermissions("user:user:detail")
    public DataResult detail(@PathVariable String userId) {
        return DataResult.success(userService.getById(userId));
    }

    @PutMapping("/changePassword")
    @ApiOperation(value = "修改密码接口")
    @Logging(title = "用户管理", describe = "修改密码", type = BusinessType.EDIT)
    public DataResult changePassword(@RequestBody PasswordVo vo) {
        vo.setUserId(httpSessionUtils.getCurrentUserId());
        userService.changePassword(vo);
        return DataResult.success();
    }

    @GetMapping("/user/roles/{userId}")
    @ApiOperation(value = "赋予角色-获取用户所属角色接口")
    @Logging(title = "用户管理", describe = "赋予角色-获取用户所属角色", type = BusinessType.QUERY)
    @RequiresPermissions("user:user:role:detail")
    public DataResult getUserOwnRole(@PathVariable String userId) {
        DataResult result = DataResult.success();
        result.setData(userService.getUserOwnRole(userId));
        return result;
    }

    @PostMapping("/user/roles")
    @ApiOperation(value = "赋予角色-设置用户角色接口")
    @Logging(title = "用户管理", describe = "赋予角色-设置用户角色", type = BusinessType.QUERY)
    @RequiresPermissions("user:user:update:role")
    public DataResult setUserOwnRole(@RequestBody UserRoleVo vo) {
        userService.setUserOwnRole(vo);
        return DataResult.success();
    }

}

